package com.taomu.app.zongchouhuli.moudle.mine.fragment;

import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.framework.BaseFragment;
import com.taomu.app.zongchouhuli.http.res.ResBankEntity;
import com.taomu.app.zongchouhuli.http.res.ResMyBankListEntity;
import com.taomu.app.zongchouhuli.moudle.mine.contract.UserCashContract;
import com.taomu.app.zongchouhuli.moudle.mine.contract.impl.UserCashPresenter;
import com.taomu.app.zongchouhuli.utils.LoadingDialog;
import com.taomu.app.zongchouhuli.utils.TokenDialog;
import com.taomu.app.zongchouhuli.utils.dialog.SelectBankDialog;
import com.taomu.app.zongchouhuli.utils.entity.SelectBankEntity;

import java.util.ArrayList;
import java.util.List;

public class CashFragment extends BaseFragment<UserCashContract.Presenter> implements UserCashContract.View {

    private LinearLayout mSelectLin;

    private TextView mBankNumTv;

    private EditText mPriceEt;

    private TextView mSubmitTv;

    public static CashFragment getInstence(){
        return  new CashFragment();
    }

    @Override
    public void onDestroy() {
        mPresenter.onDetch();
        super.onDestroy();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_cash;
    }

    @Override
    protected void initView(View view) {
        mDialog = LoadingDialog.createLoadingDialog(getContext(), "加载中...");
        mSelectLin = view.findViewById(R.id.cash_select_bank_lin);
        mPriceEt = view.findViewById(R.id.cash_price_et);
        mBankNumTv = view.findViewById(R.id.cash_banknum_tv);
        mSubmitTv = view.findViewById(R.id.cash_next_et);
    }

    private List<SelectBankEntity> entities;

    @Override
    protected void initData() {
        entities = new ArrayList<>();
        for(int i=0;i<3;i++){
            SelectBankEntity entity = new SelectBankEntity();
            entity.setBankName("中国银行");
            entity.setBankNum("51221236525412365555");
            entities.add(entity);
        }
        mPresenter.getBankList();
        mPresenter.isNextCanClick(mPriceEt,mBankNumTv);
    }

    @Override
    protected void initEvent() {
        mSelectLin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSelectDialog();
            }
        });
        mSubmitTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.getCrash(mPriceEt.getText().toString(), String.valueOf(selectData.getId()));
            }
        });
    }

    @Override
    protected UserCashContract.Presenter createPresenter() {
        return new UserCashPresenter(this);
    }

    private SelectBankDialog bankdialog;

    private SelectBankEntity selectData;

    private void showSelectDialog(){
        if(bankdialog == null) {
            bankdialog = new SelectBankDialog();
            bankdialog.setData(entities,selectData);
            bankdialog.setSelectBankDialogListener(new SelectBankDialog.SelectBankDialogListener() {
                @Override
                public void onDismiss(SelectBankEntity entity) {
                    String bankNum = entity.getBankNum();
                    selectData = entity;
                    mBankNumTv.setText(bankNum.substring(0, 4) + " **** **** **** " + bankNum.substring(bankNum.length() - 4, bankNum.length()));
                }
            });
            bankdialog.show(getChildFragmentManager(), "select");
        }else{
            //java.lang.IllegalStateException: DialogFragment can not be attached to a container view
            bankdialog.setData(entities,selectData);
            bankdialog.show(getChildFragmentManager(),"select");
        }
    }

    @Override
    public void setNextCanClick(int bgcolor, boolean enable) {
        mSubmitTv.setBackgroundResource(bgcolor);
        mSubmitTv.setEnabled(enable);
    }

    @Override
    public void setSubmitResults(String msg) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_forget_tip, null);
        final Dialog dialog = new Dialog(getContext());
        final TextView tv1 = view.findViewById(R.id.tipTextView);
        tv1.setText("提现申请成功");
        dialog.setContentView(view);
        TextView tv = view.findViewById(R.id.dialog_confirm_tv);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    @Override
    public void setBankList(ResMyBankListEntity resProductEntities) {
        entities.clear();
        if(resProductEntities.getData() != null){
            for(int i=0;i<resProductEntities.getData().size();i++){
                SelectBankEntity entity = new SelectBankEntity();
                entity.setBankName(resProductEntities.getData().get(i).getCard_username());
                entity.setBankNum(resProductEntities.getData().get(i).getCard_num());
                entity.setId(resProductEntities.getData().get(i).getId());
                entities.add(entity);
            }
        }
    }

    @Override
    public void setPresenter(UserCashContract.Presenter presenter) {

    }

    private Dialog mDialog;

    @Override
    public void showLoadingDialog() {
        mDialog.show();
    }

    @Override
    public void dismissLoadingDialog() {
        LoadingDialog.closeDialog(mDialog);
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showToken() {
        TokenDialog.getInsence(getContext()).show();
    }
}
